
#include "OpenGLWindow.h"

COpenGLWindow::COpenGLWindow(void)
{
}

COpenGLWindow::~COpenGLWindow(void)
{
}

void COpenGLWindow::AttachWindow( HWND hWnd )
{
	static	PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,							
		PFD_DRAW_TO_WINDOW |		
		PFD_SUPPORT_OPENGL |		
		PFD_DOUBLEBUFFER,			
		PFD_TYPE_RGBA,				
		24,						
		0, 0, 0, 0, 0, 0,			
		0,							
		0,							
		0,							
		0, 0, 0, 0,					
		24,
		0,							
		0,							
		PFD_MAIN_PLANE,				
		0,							
		0, 0, 0	
	};
	
	m_hdc = GetDC( hWnd );

	int PixelFormat = ChoosePixelFormat(m_hdc,&pfd);
	SetPixelFormat( m_hdc,PixelFormat,&pfd );
	m_hglrc = wglCreateContext( m_hdc );
	wglMakeCurrent( m_hdc,m_hglrc );

	this->LoadExtensions();
}

void COpenGLWindow::SetClearColor( float r,float g,float b,float a)
{
	glClearColor( r,g,b,a);
}

void COpenGLWindow::MakeCurrent()
{
	wglMakeCurrent( m_hdc,m_hglrc );
}

void COpenGLWindow::BeginScene()
{
	glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
}

void COpenGLWindow::EndScene()
{
	SwapBuffers( m_hdc );
}

void COpenGLWindow::ResizeView( int cx,int cy )
{
	MakeCurrent();

	if( cy == 0 )
		cy = 1;

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 75.f,1.f,5.f,16777215.f);
	glViewport( 0,0,cx,cy);

	glMatrixMode( GL_MODELVIEW );
}

#include "Renderer\OpenGLWindow.h"

void COpenGLWindow::LoadExtensions(void)
{
	glLockArraysEXT = (PFNGLLOCKARRAYSEXTPROC) wglGetProcAddress("glLockArraysEXT");
    glUnlockArraysEXT = (PFNGLUNLOCKARRAYSEXTPROC) wglGetProcAddress("glUnlockArraysEXT");

	glMultiTexCoord2fARB = ( PFNGLMULTITEXCOORD2FARBPROC ) wglGetProcAddress("glMultiTexCoord2fARB");
	glMultiTexCoord2fvARB = ( PFNGLMULTITEXCOORD2FVARBPROC ) wglGetProcAddress("glMultiTexCoord2fvARB");
	glMultiTexCoord3fARB = ( PFNGLMULTITEXCOORD3FARBPROC ) wglGetProcAddress("glMultiTexCoord3fARB");
	glMultiTexCoord3fvARB = ( PFNGLMULTITEXCOORD3FVARBPROC ) wglGetProcAddress("glMultiTexCoord3fvARB");

	glActiveTextureARB = ( PFNGLACTIVETEXTUREARBPROC ) wglGetProcAddress("glActiveTextureARB");
	glClientActiveTextureARB = ( PFNGLCLIENTACTIVETEXTUREARBPROC) wglGetProcAddress("glClientActiveTextureARB"); 

	glUniform1f = (PFNGLUNIFORM1FPROC )wglGetProcAddress("glUniform1f");
	glUniform2f = (PFNGLUNIFORM2FPROC )wglGetProcAddress("glUniform2f");
	glUniform3f = (PFNGLUNIFORM3FPROC )wglGetProcAddress("glUniform3f");
	glUniform4f = (PFNGLUNIFORM4FPROC )wglGetProcAddress("glUniform4f");
	glUniform1i = (PFNGLUNIFORM1IPROC )wglGetProcAddress("glUniform1i");
	glUniform1uiEXT = (PFNGLUNIFORM1UIEXTPROC )wglGetProcAddress("glUniform1uiEXT");

	glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC )wglGetProcAddress("glUseProgramObjectARB");
	glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC )wglGetProcAddress("glCreateShaderObjectARB");
	glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC )wglGetProcAddress("glShaderSourceARB");
	glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC )wglGetProcAddress("glCompileShaderARB");
	glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC )wglGetProcAddress("glGetObjectParameterivARB");
	glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC )wglGetProcAddress("glGetInfoLogARB");
	glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC )wglGetProcAddress("glCreateProgramObjectARB");
	glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC )wglGetProcAddress("glAttachObjectARB");
	glLinkProgramARB = (PFNGLLINKPROGRAMARBPROC )wglGetProcAddress("glLinkProgramARB");
	glDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC )wglGetProcAddress("glDeleteObjectARB");
	glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC )wglGetProcAddress("glGetUniformLocation");

	glUniformMatrix2fvARB  = (PFNGLUNIFORMMATRIX2FVPROC )wglGetProcAddress("glUniformMatrix2fvARB");
	glUniformMatrix3fvARB  = (PFNGLUNIFORMMATRIX3FVPROC )wglGetProcAddress("glUniformMatrix3fvARB");
	glUniformMatrix4fvARB  = (PFNGLUNIFORMMATRIX4FVPROC )wglGetProcAddress("glUniformMatrix4fvARB");

	glWeightfvARB = (PFNGLWEIGHTFVARBPROC)wglGetProcAddress("glWeightfvARB");
	glWeightPointerARB = (PFNGLWEIGHTPOINTERARBPROC)wglGetProcAddress("glWeightPointerARB");
	glVertexBlendARB = (PFNGLVERTEXBLENDARBPROC)wglGetProcAddress("glVertexBlendARB");

	glVertexAttrib1fARB = (PFNGLVERTEXATTRIB1FARBPROC)wglGetProcAddress("glVertexAttrib1fARB");
	glVertexAttrib2fARB = (PFNGLVERTEXATTRIB2FARBPROC)wglGetProcAddress("glVertexAttrib2fARB");
	glVertexAttrib3fARB = (PFNGLVERTEXATTRIB3FARBPROC)wglGetProcAddress("glVertexAttrib3fARB");
	glVertexAttrib4fARB = (PFNGLVERTEXATTRIB4FARBPROC)wglGetProcAddress("glVertexAttrib4fARB");
	glVertexAttribPointerARB = (PFNGLVERTEXATTRIBPOINTERARBPROC)wglGetProcAddress("glVertexAttribPointerARB");
	glEnableVertexAttribArrayARB = (PFNGLENABLEVERTEXATTRIBARRAYARBPROC)wglGetProcAddress("glEnableVertexAttribArrayARB");
	glDisableVertexAttribArrayARB = (PFNGLDISABLEVERTEXATTRIBARRAYARBPROC)wglGetProcAddress("glDisableVertexAttribArrayARB");
	glBindAttribLocationARB = (PFNGLBINDATTRIBLOCATIONARBPROC)wglGetProcAddress("glBindAttribLocationARB");
	glGetActiveAttribARB = (PFNGLGETACTIVEATTRIBARBPROC)wglGetProcAddress("glGetActiveAttribARB");
	glGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)wglGetProcAddress("glGetAttribLocationARB");

}